package com.ljb.calendar.month;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.ljb.calendar.DateViewAdapter;
import com.ljb.calendar.bean.DateBean;
import com.ljb.calendar.OnDateViewClickListener;

import org.joda.time.LocalDate;

import java.util.ArrayList;

/**
 * Created by ljb on 2018/3/21.
 */

public class MonthView extends LinearLayout {
    private DateViewAdapter mDateViewAdapter;
    private OnDateViewClickListener mOnDateViewClickListener;
    private DateBean mStartDateBean;

    public MonthView(Context context) {
        this(context, null);
    }

    public MonthView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MonthView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setWillNotDraw(true);
        setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        setOrientation(VERTICAL);
    }

    public void setDateViewAdapter(DateViewAdapter dateViewAdapter) {
        mDateViewAdapter = dateViewAdapter;
    }

    public void setOnDateViewClickListener(OnDateViewClickListener onDateViewClickListener) {
        mOnDateViewClickListener = onDateViewClickListener;
    }

    /**
     * 获取当前月1日的DateBean对象
     * @return
     */
    public DateBean getStartDateBean() {
        return mStartDateBean;
    }

    public void setData(LocalDate mTodayDate, int year, int month, int position) {
        if (mDateViewAdapter == null)
            return;

        ArrayList<DateBean> localDates = initDate(mTodayDate, year, month, position);
        intiDateView(localDates);
    }

    private void intiDateView(ArrayList<DateBean> localDates) {
        LinearLayout row = null;
        for (int i = 0; i < 42; i++) {
            DateBean dateBean = localDates.get(i);

            if (i % 7 == 0) {
                row = new LinearLayout(getContext());
                row.setHorizontalGravity(HORIZONTAL);
                LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1f);
                addView(row, params);
            }

            View dateView = mDateViewAdapter.getDateView(this, dateBean);
            LayoutParams params = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1f);
            if (row != null) {
                row.addView(dateView, params);
            }

            //添加点击事件
            setItemClick(dateView, dateBean);
        }
    }

    private void setItemClick(View dateView, final DateBean bean) {
        if (mOnDateViewClickListener != null)
            dateView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnDateViewClickListener.onDateViewClick(v, bean);
                }
            });
    }

    private ArrayList<DateBean> initDate(LocalDate todayDate, int year, int month, int position) {
        ArrayList<DateBean> dateBeans = new ArrayList<>();
        //获取当本月第一天
        LocalDate firstDay = new LocalDate(year, month, 1);
        //获取本月最后一天
        LocalDate lastDay = firstDay.dayOfMonth().withMaximumValue();

        //获取上一个月需要显示的日期
        int fCount = firstDay.getDayOfWeek();
        if (fCount == 7)
            fCount = 0;
        for (int i = fCount; i > 0; i--) {
            LocalDate date = firstDay.plusDays(-i);
            DateBean bean = new DateBean();
            bean.setLocalDate(date);
            bean.setTodayDate(todayDate);
            bean.setPositionOfPager(position);
            bean.setMonthFlag(-1);
            dateBeans.add(bean);
        }

        //获取当前月需要显示的日期
        int total = lastDay.getDayOfMonth();
        for (int i = 0; i < total; i++) {
            LocalDate date = firstDay.plusDays(i);
            DateBean bean = new DateBean();
            bean.setLocalDate(date);
            bean.setTodayDate(todayDate);
            bean.setPositionOfPager(position);
            bean.setMonthFlag(0);
            dateBeans.add(bean);

            if (i == 0) {
                mStartDateBean = bean;
            }
        }

        //获取下一个月需要显示的日期
        int BCount = 42 - fCount - total;
        for (int i = 1; i <= BCount; i++) {
            LocalDate date = lastDay.plusDays(i);
            DateBean bean = new DateBean();
            bean.setLocalDate(date);
            bean.setTodayDate(todayDate);
            bean.setPositionOfPager(position);
            bean.setMonthFlag(1);
            dateBeans.add(bean);
        }
        return dateBeans;
    }
}
